package net.pzc.filechooser;

import java.awt.Cursor;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.SwingUtilities;

public class MergeCmbAction extends Thread {

	private JComboBox ui = null;
	private DefaultComboBoxModel model = null;
	private String initPath = null;
	private FastFileChooser chooser = null;

	public MergeCmbAction(JComboBox ui, String initPath, FastFileChooser chooser) {
		this.ui = ui;
		this.chooser = chooser;
		ui.setEnabled(false);
		ui.getParent().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		this.model = (DefaultComboBoxModel) ui.getModel();
		this.initPath = initPath;
	}

	@Override
	public void run() {
		
		int selectedIndex = -1;

		boolean flg = false;
		for (int i = 0; i < model.getSize(); i++) {
			FileItem fi = (FileItem) model.getElementAt(i);
			if (fi.getAbsolutePath().equalsIgnoreCase(initPath)) {
				selectedIndex = i;
				flg = true;
				break;
			}
		}

		if(flg == false){

			List<String> pList = new ArrayList<String>();
			File f = new File(initPath);
			while (true) {
				pList.add(0, f.getAbsolutePath());
				if (f.getParentFile() == null) {
					break;
				}
				f = f.getParentFile();
			}

			int maxStep = -1;
			int index = -1;
			int indexJ = -1;
			for (int i = 0; i < model.getSize(); i++) {
				FileItem fi = (FileItem) model.getElementAt(i);
				for (int j = 0; j < pList.size(); j++) {
					if (fi.getAbsolutePath().equalsIgnoreCase(pList.get(j))) {
						if (fi.getStep() > maxStep) {
							maxStep = fi.getStep();
							index = i;
							indexJ = j;
						}
					}
				}
			}

			if (maxStep == -1) {
				index = model.getSize() - 1;
				indexJ = -1;
			}

			int step = maxStep;
			for (int j = indexJ + 1; j < pList.size(); j++) {
				File file = new File(pList.get(j));
				FileItem fi = new FileItem();

				fi.setAbsolutePath(file.getAbsolutePath());
				fi.setName(file.getName());
				fi.setStep(++step);

				if(j == (pList.size() - 1)){
					FastFileChooser.make(file, fi, true);
				}else{
					FastFileChooser.make(file, fi, false);
				}

				try {
					index++;
					updateUI(fi, index);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}

			selectedIndex = index;
		}
		
		try {
			finishWork(selectedIndex);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void updateUI(final FileItem fi, final int index) throws Exception {
		SwingUtilities.invokeAndWait(new Runnable() {

			public void run() {
				model.insertElementAt(fi, index);
			}

		});
	}

	private void finishWork(final int index) throws Exception {
		SwingUtilities.invokeAndWait(new Runnable() {

			public void run() {
				ui.getParent().setCursor(Cursor.getDefaultCursor());
				ui.setEnabled(true);
				if (index > -1) {
					ui.setSelectedIndex(index);
					chooser.rescanCurrentDirectory();
				}
			}

		});
	}
}